package com.boot.security.service.cluster;

import com.boot.security.model.vo.SourceKey;
import com.boot.security.service.authorization.AccessSource;
import com.boot.web.http.HttpModel;
import com.boot.web.util.HttpResponseUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/cluster")
@Api("集群接口")
public class DefaultAccessSourceClusterHandlerImpl {

    private final DiscoveryClient client;

    @Value("${spring.application.name}")
    private String applicationName;

    private final AccessSource accessSource;

    public DefaultAccessSourceClusterHandlerImpl(DiscoveryClient client, AccessSource accessSource) {
        this.client = client;
        this.accessSource = accessSource;
    }

    @GetMapping("/list")
    public ResponseEntity<HttpModel<Void>> getServices() {
        List<ServiceInstance> serviceInstances = client.getInstances(applicationName);
        serviceInstances.forEach(serviceInstance -> {
            System.out.println(serviceInstance.getServiceId());
            System.out.println(serviceInstance.getInstanceId());
            System.out.println(serviceInstance.getHost());
            System.out.println(serviceInstance.getPort());
            System.out.println(serviceInstance.getUri());
            System.out.println(serviceInstance.getScheme());
            System.out.println(serviceInstance.getMetadata());
        });
        return HttpResponseUtil.OK("查询成功，无效返回");
    }

    @PostMapping("/save")
    public ResponseEntity<HttpModel<Void>> save(@RequestBody AccessSourceClusterChange event) {
        SourceKey currentSourceKey = accessSource.getSourceKey();
        SourceKey eventSourceKey = event.getSourceKey();
        if (eventSourceKey.equals(currentSourceKey)) {
            log.info("收到了本机发送的规则修正请求，已忽略该请求!");
        } else if (currentSourceKey.getTime().isAfter(eventSourceKey.getTime())) {
            log.warn("收到了过期的规则修正请求，已忽略该请求!");
        } else {
            accessSource.saveOne(event.getResourceId(), eventSourceKey);
        }
        return HttpResponseUtil.OK("保存成功");
    }

    @PostMapping("/save-list")
    public ResponseEntity<HttpModel<Void>> saveList(@RequestBody AccessSourceClusterChange event) {
        SourceKey currentSourceKey = accessSource.getSourceKey();
        SourceKey eventSourceKey = event.getSourceKey();
        if (eventSourceKey.equals(currentSourceKey)) {
            log.info("收到了本机发送的规则修正请求，已忽略该请求!");
        } else if (currentSourceKey.getTime().isAfter(eventSourceKey.getTime())) {
            log.warn("收到了过期的规则修正请求，已忽略该请求!");
        } else {
            accessSource.saveMulti(event.getResourceIds(), eventSourceKey);
        }
        return HttpResponseUtil.OK("保存成功");
    }

    @PostMapping("/update")
    public ResponseEntity<HttpModel<Void>> update(@RequestBody AccessSourceClusterChange event) {
        SourceKey currentSourceKey = accessSource.getSourceKey();
        SourceKey eventSourceKey = event.getSourceKey();
        if (eventSourceKey.equals(currentSourceKey)) {
            log.info("收到了本机发送的规则修正请求，已忽略该请求!");
        } else if (currentSourceKey.getTime().isAfter(eventSourceKey.getTime())) {
            log.warn("收到了过期的规则修正请求，已忽略该请求!");
        } else {
            accessSource.updateOne(event.getResource(), eventSourceKey);
        }
        return HttpResponseUtil.OK("更新成功");
    }

    @PostMapping("/remove")
    public ResponseEntity<HttpModel<Void>> remove(@RequestBody AccessSourceClusterChange event) {
        SourceKey currentSourceKey = accessSource.getSourceKey();
        SourceKey eventSourceKey = event.getSourceKey();
        if (eventSourceKey.equals(currentSourceKey)) {
            log.info("收到了本机发送的规则修正请求，已忽略该请求!");
        } else if (currentSourceKey.getTime().isAfter(eventSourceKey.getTime())) {
            log.warn("收到了过期的规则修正请求，已忽略该请求!");
        } else {
            accessSource.removeOne(event.getResource(), eventSourceKey);
        }
        return HttpResponseUtil.OK("删除成功");
    }

    @PostMapping("/remove-list")
    public ResponseEntity<HttpModel<Void>> removeList(@RequestBody AccessSourceClusterChange event) {
        SourceKey currentSourceKey = accessSource.getSourceKey();
        SourceKey eventSourceKey = event.getSourceKey();
        if (eventSourceKey.equals(currentSourceKey)) {
            log.info("收到了本机发送的规则修正请求，已忽略该请求!");
        } else if (currentSourceKey.getTime().isAfter(eventSourceKey.getTime())) {
            log.warn("收到了过期的规则修正请求，已忽略该请求!");
        } else {
            accessSource.removeMulti(event.getResources(), eventSourceKey);
        }
        return HttpResponseUtil.OK("删除成功");
    }


}
